package com.kobe.algorithm.leetcode.case5;

/**
 * @author ：caopengfei
 * @date ：2021/2/23
 *
 * {
 * P(i,i)=true
 * P(i,i+1)=(Si==Si+1)
 * ​	}
 * 动态规划
 * P(i,j)=P(i+1,j−1)∧(Si ==Sj)
 */
public class Solution {

    public String longestPalindrome(String s) {
        int len = s.length();
        if(len < 2){
           return s;
        }
        int startIndex = 0;
        int maxLen = 1;
        char [] charArr = s.toCharArray();
        for(int i = 0; i < s.length()-1;i++){
            for(int j = i+1; j <s.length(); j++){
                System.out.println("sub-----"+s.substring(i,j+1));
                if( (j-i+1) > maxLen && validPalindromicSubstring(charArr,i,j)){
                    startIndex = i;
                    maxLen = j-i+1;

                }
            }
        }
        return s.substring(startIndex,startIndex+maxLen);
    }

    private boolean validPalindromicSubstring(char [] chars,int left,int right){
        while(left  < right){
            if(chars[left] != chars[right]){
                return false;
            }
            left++;
            right--;
        }
        return true;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println("result----"+solution.longestPalindrome("ac"));
    }
}
